package com.resjz.milddog.service.user;

import com.purgeteam.cloud.dispose.starter.Result;
import com.resjz.milddog.Interface.menu.MenuInterface;
import com.resjz.milddog.Interface.role.RoleInterface;
import com.resjz.milddog.Interface.user.UserInterface;
import com.resjz.milddog.vo.Role.UserRoleVo;
import com.resjz.milddog.vo.menu.MenuVo;
import com.resjz.milddog.vo.user.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserService {
    //引入dubbo
    @DubboReference(version = "${dubbo.service.version}",check = false)
    private UserInterface userInterface;
    @DubboReference(version = "${dubbo.service.version}",check = false)
    private RoleInterface roleInterface;
    @DubboReference(version = "${dubbo.service.version}",check = false)
    private MenuInterface menuInterface;

    public UserVo getUserInfoByZId(Long id){
        UserVo user = userInterface.getUserByUserId(id);
        return user;
    }

    public Result getusermenu (Long userId){
        List<Map<String,Object>> menudata = new ArrayList<>();
        List<Map<String,Object>> routerdata = new ArrayList<>();
        Map<String,Object> backdata = new HashMap<>();
        List<UserRoleVo> userRoles = roleInterface.getUserRoles(userId);
        if(userRoles.size()>0){
            List<Long> menuidsarr = new ArrayList<>();
            for (UserRoleVo ur : userRoles) {
                menuidsarr.addAll(menuInterface.getRoleMenu(ur.getRoleId()));
            }
            List<MenuVo> menuListInArr = menuInterface.getMenuListInArr(menuidsarr);
            List<MenuVo> mainmenulist = menuListInArr.stream().filter(ori->ori.getParentId()==0).collect(Collectors.toList());
            Map<Long, List<MenuVo>> collect = menuListInArr.stream().collect(Collectors.groupingBy(MenuVo::getParentId));

            for (MenuVo menuVo : mainmenulist) {
                Map<String,Object> temp = new HashMap<>();
                temp.put("icon",menuVo.getIcon());
                temp.put("title",menuVo.getTitle());
                temp.put("type",menuVo.getType());
                Map<String,Object> router = new HashMap<>();
                router.put("path","/"+menuVo.getPath());
                router.put("name",menuVo.getPath());
                findMenuSubMenu(menuVo,collect,temp,router);
                menudata.add(temp);
                routerdata.add(router);
            }
        }
        backdata.put("menus",menudata);
        backdata.put("routers",routerdata);
        return Result.ofSuccess(backdata);
    }
    private void findMenuSubMenu(MenuVo menuVo,Map<Long, List<MenuVo>> collect,Map<String,Object> temp,Map<String,Object> router){
        Optional<List<MenuVo>> menuvotemp = Optional.ofNullable(collect.get(menuVo.getMenuId()));
        menuvotemp.ifPresent(menuVos -> {
            List<Map<String,Object>> childata = new ArrayList<>();
            List<Map<String,Object>> childrouter = new ArrayList<>();
            for (MenuVo vo : menuVos) {
                Map<String,Object> subtemp = new HashMap<>();
                subtemp.put("path",String.valueOf(router.get("path"))+"/"+vo.getPath());
                subtemp.put("icon",vo.getIcon());
                subtemp.put("title",vo.getTitle());
                subtemp.put("type",vo.getType());
                if(vo.getType()==1){
                    Map<String,Object> rou = new HashMap<>();
                    rou.put("path",vo.getPath());
                    rou.put("name",vo.getPath());
                    rou.put("meta",new HashMap<String,Object>(){{
                        put("title",vo.getTitle());
                        put("auth",true);
                    }});
                    rou.put("component",(String.valueOf(router.get("path"))+"/"+vo.getPath()).substring(1));
                    childrouter.add(rou);
                }
                childata.add(subtemp);
                findMenuSubMenu(vo,collect,subtemp,router);
            }
            if (router.containsKey("children")) {
                List<Map<String, Object>> tmep = (List<Map<String, Object>>) router.get("children");
                router.put("children", tmep.addAll(childrouter));
            } else {
                router.put("children", childrouter);
            }
            temp.put("children",childata);
        });
    }
}
